package com.start.common.dataRule;


import com.alibaba.fastjson2.JSONObject;
import com.start.common.entity.StartBaseMenuDataRules;
import com.start.common.tools.SpringContextTools;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 通用查询条件
 */
public class StartQueryWrapper extends AbstractCommonQuery<Object> {


    private Map<String, String[]> entityData;


    public StartQueryWrapper() {
        super(SpringContextTools.getHttpServletRequest());
    }

    public StartQueryWrapper(JSONObject data) {
        super(SpringContextTools.getHttpServletRequest());
        if (data == null || data.isEmpty()) {
            return;
        }
        entityData = new HashMap<>();
        for (String key : data.keySet()) {
            entityData.put(key, new String[]{data.getString(key)});
        }


    }

    public StartQueryWrapper(Map<String, String[]> data) {
        super(SpringContextTools.getHttpServletRequest());
        entityData = data;
    }


    /**
     * 开启驼峰名命名
     *
     * @return
     */
    @Override
    public boolean enableHumpNaming() {
        return false;
    }


    /**
     * 检查合法性
     *
     * @param dataRulesVos
     */
    @Override
    public void checkLegitimacy(List<StartBaseMenuDataRules> dataRulesVos) {

    }

    /**
     * 解析搜索条件
     *
     * @param prefix 前缀
     */
    @Override
    public void analysisSearchQuery(String prefix) {
        if (entityData == null || entityData.size() < 1) return;
        if (StringUtils.isEmpty(prefix)) prefix = "";
        Set<String> keys = entityData.keySet();
        for (String objKey : keys) {
            if (!objKey.startsWith(prefix)) {
                continue;
            }
            String[] columnValues = entityData.getOrDefault(objKey, null);
            if (columnValues == null || columnValues.length < 1) return;
            if (StringUtils.isEmpty(columnValues[0])) continue;
            DataRuleConditionEnum ruleCondition = DataRuleConditionEnum.getRuleCondition(columnValues);
            if (ruleCondition == null) ruleCondition = DataRuleConditionEnum.EQ;
            constructorCondition(objKey.toString().replace(prefix, ""), columnValues[0], ruleCondition, enableHumpNaming());
        }
    }

    @Override
    public void analysisQueryOrder(String prefix) {
        Set<String> keys = entityData.keySet();
        for (String objKey : keys) {
            if (!objKey.startsWith(prefix)) {
                continue;
            }
            String[] columnValues = entityData.getOrDefault(objKey, null);
            if (columnValues == null || columnValues.length < 1) continue;
            DataRuleConditionEnum conditionEnum = DataRuleConditionEnum.ORDER_BY_ASC;
            if (columnValues[0].trim().equalsIgnoreCase("desc")) {
                conditionEnum = DataRuleConditionEnum.ORDER_BY_DESC;
            }
            constructorCondition(objKey.replace(prefix, ""), null, conditionEnum, enableHumpNaming());
        }
    }

    /**
     * 构建条件
     *
     * @param columnName
     * @param columnValue
     * @param conditionEnum
     * @param enableHumpNaming
     */
    @Override
    protected void constructorCondition(String columnName, String columnValue, DataRuleConditionEnum conditionEnum, boolean enableHumpNaming) {

        String[] split;
        switch (conditionEnum) {
            case EQ://等于
                this.eq(columnName, columnValue);
                break;
            case GT://大于
                this.gt(columnName, columnValue);
                break;
            case GE://大于等于
                this.ge(columnName, columnValue);
                break;
            case LT://小于
                this.lt(columnName, columnValue);
                break;
            case LE://小于等于
                this.le(columnName, columnValue);
                break;
            case NE://不等于
                this.ne(columnName, columnValue);
                break;
            case BETWEEN://之间
                split = columnValue.split(",");
                if (split.length < 2) {
                    throw new RuntimeException("构建条件查询失败 between :参数：" + columnValue);
                }
                this.between(columnName, split[0], split[1]);
                break;
            case NOT_BETWEEN://不在之间
                split = columnValue.split(",");
                if (split.length < 2) {
                    throw new RuntimeException("构建条件查询失败 notBetween :参数：" + columnValue);
                }
                this.notBetween(columnName, split[0], split[1]);
                break;
            case LIKE://包含
                this.like(columnName, columnValue);
                break;
            case NOT_LIKE://不包含
                this.notLike(columnName, columnValue);
                break;
            case LIKE_LEFT://左模糊
                this.likeLeft(columnName, columnValue);
                break;
            case LIKE_RIGHT://右模糊
                this.likeRight(columnName, columnValue);
                break;
            case ORDER_BY_ASC://正序
                this.orderByAsc(columnName);
                break;
            case ORDER_BY_DESC://倒序
                this.orderByDesc(columnName);
                break;
            case IN:
            case NOT_IN:
                final List<String> collect = Arrays.stream(columnValue.split(",")).distinct().collect(Collectors.toList());
                if (collect.size() < 1) break;
                if (conditionEnum == DataRuleConditionEnum.IN) {
                    this.in(columnName, collect);
                } else {
                    this.notIn(columnName, collect);
                }
                break;
            case IS_NULL:
                this.isNull(columnName);
                break;
            case IS_NOT_NULL:
                this.isNotNull(columnName);
                break;
            case CUSTOM_SQL://自定义sql
                this.apply(StringUtils.isNoneEmpty(columnValue), columnValue);
                break;
        }
    }


}
